package com.applink.sell_proof.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.net.Uri;

import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.database.table.ClaimDBHelper;
import com.applink.sell_proof.util.Utility;

public class Claim {

	public static final String TYPE_PROJECT = "Project Claim";
	public static final String TYPE_UNIT = "Unit Claim";

	private long id = 0;
	private String mLocalId = "";
	private String ClaimStatus = "";
	private String opportunityName = "";
	private String invoiceNo;
	private String mCustomerCompanyName;
	private long date;
	private String type;
	private String opportunityId = "";
	private long mDistributorId = 0;
	private double total = 0;

	private List<Product> products = new ArrayList<Product>();

	private List<ClaimImage> images = new ArrayList<ClaimImage>();

	public long getId() {
		return id;
	}

	public void setId(long id) {
		remeberUpdated(ClaimDBHelper.ID);
		this.id = id;
	}

	public String getInvoiceNo() {
		return invoiceNo;
	}

	public void setInvoiceNo(String invoiceNo) {
		remeberUpdated(ClaimDBHelper.INVOICE_NO);
		this.invoiceNo = invoiceNo;
	}

	public String getCustomerCompanyName() {
		return mCustomerCompanyName;
	}

	public void setCustomerCompanyName(String customerName) {
		remeberUpdated(ClaimDBHelper.CUSTOMER_COMPANY_NAME);
		this.mCustomerCompanyName = customerName;
	}

	public long getDate() {
		return date;
	}

	public void setDate(long date) {
		remeberUpdated(ClaimDBHelper.DATE);
		this.date = date;
	}

	/**
	 * Special handle for input a date string out put a Unix timestamp long
	 * 
	 * @param s
	 */
	public void setDate(String s) {

		if ((s == null) || s.equals("") || s.equals("null"))
			return;

		try {
			Date d = Utility.findDate(s);
			this.date = d.getTime();
			remeberUpdated(ClaimDBHelper.DATE);
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println(" ===== Invalid Date ===== " + s);
		}
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		remeberUpdated(ClaimDBHelper.TYPE);
		this.type = type;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		remeberUpdated(ClaimDBHelper.PRODUCTS);
		this.products = products;
	}

	/**
	 * 
	 * @return
	 */
	public List<ClaimImage> getImages() {
		return images;
	}

	/**
	 * 
	 * @return
	 */
	public List<Uri> getImagesUri() {

		List<Uri> uris = new ArrayList<Uri>();

		if (this.images.size() > 0)
			for (ClaimImage ci : this.images)
				uris.add(ci.getImageLocation());

		return uris;
	}

	public void setImages(List<ClaimImage> images) {
		remeberUpdated(ClaimDBHelper.IMAGES);
		this.images = images;
	}

	public String getOpportunityId() {
		return opportunityId;
	}

	public void setOpportunityId(String opportunityId) {
		remeberUpdated(ClaimDBHelper.PROJECT_ID);
		this.opportunityId = opportunityId;
	}

	public void setDistributorId(long distributorId) {
		remeberUpdated(ClaimDBHelper.DISTRIBUTOR_ID);
		this.mDistributorId = distributorId;
	}

	public long getDistributorId() {
		return this.mDistributorId;
	}

	public String getOpportunityName() {
		return opportunityName;
	}

	public void setOpportunityName(String opportunityName) {
		remeberUpdated(ClaimDBHelper.PROJECT_NAME);
		this.opportunityName = opportunityName;
	}

	public String getStatus() {
		return ClaimStatus;
	}

	public void setStatus(String claimStatus) {
		remeberUpdated(ClaimDBHelper.STATUS);
		ClaimStatus = claimStatus;
	}

	/**
	 * @return the mLocalId
	 */
	public String getLocalId() {
		return mLocalId;
	}

	/**
	 * @param mLocalId
	 *            the mLocalId to set
	 */
	public void setLocalId(String mLocalId) {
		remeberUpdated(Constant.ID_LOCAL);
		this.mLocalId = mLocalId;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		remeberUpdated(ClaimDBHelper.TOTAL);
		this.total = total;
	}

	private ArrayList<String> mUpdatedfilelds = new ArrayList<String>();

	public void remeberUpdated(String field) {
		if (!mUpdatedfilelds.contains(field))
			mUpdatedfilelds.add(field);
	}

	public boolean isUpdated(String field) {
		return mUpdatedfilelds.contains(field);
	}
}
